package com.ms.controller;

import com.ms.entity.Organizer;
import com.ms.entity.PlaceApplication;
import com.ms.service.inter.PlaceApplicationService;
import com.ms.utils.SimpleTokenUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/place_app")
public class PlaceApplicationController {
    
    @Resource
    private PlaceApplicationService placeApplicationService;
    
    /**
     * 举办方申请借用场地
     * @param request HTTP请求，用于获取token
     * @param application 场地申请信息
     * @return 申请结果
     */
    //测试路径：localhost:8080/place_app/apply
    @PostMapping("/apply")
    public Map<String, Object> applyForPlace(HttpServletRequest request, @RequestBody PlaceApplication application) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 从token中获取当前登录的举办方信息
            String token = request.getHeader("token");
            if (token == null || token.isEmpty()) {
                result.put("success", false);
                result.put("msg", "未登录");
                return result;
            }
            
            Object userObj = SimpleTokenUtils.getUserByToken(token);
            if (!(userObj instanceof Organizer)) {
                result.put("success", false);
                result.put("msg", "无权限操作");
                return result;
            }
            
            Organizer organizer = (Organizer) userObj;
            
            // 设置申请信息
            application.setOrId(organizer.getOrId()); // 自动设置租借方ID
            application.setAppTime(new Date()); // 设置申请时间
            application.setLeaStatus(0); // 默认状态为待审核
            
            // 保存申请信息
            boolean success = placeApplicationService.savePlaceApplication(application);
            if (success) {
                result.put("success", true);
                result.put("msg", "申请提交成功，请等待审核");
            } else {
                result.put("success", false);
                result.put("msg", "申请提交失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "申请提交失败：" + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 查询当前举办方的申请记录
     * @param request HTTP请求，用于获取token
     * @return 申请记录列表
     */
    //测试路径：localhost:8080/place_app/list
    @GetMapping("/list")
    public Map<String, Object> getMyApplications(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 从token中获取当前登录的举办方信息
            String token = request.getHeader("token");
            if (token == null || token.isEmpty()) {
                result.put("success", false);
                result.put("msg", "未登录");
                return result;
            }
            
            Object userObj = SimpleTokenUtils.getUserByToken(token);
            if (!(userObj instanceof Organizer)) {
                result.put("success", false);
                result.put("msg", "无权限操作");
                return result;
            }
            
            Organizer organizer = (Organizer) userObj;
            
            // 查询当前举办方的申请记录
            result.put("success", true);
            result.put("data", placeApplicationService.getPlaceApplicationsByOrganizerId(organizer.getOrId()));
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        
        return result;
    }
}